<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
   <el-form-item label="机构" prop="tenantId">
   		<el-select v-model="queryParams.tenantId" style="width: 300px">
   		   <el-option
   		      v-for="item in orgOptions"
   		      :key="item.tenantId"
   		      :label="item.companyName"
   		      :value="item.tenantId"
   		   />
   		</el-select>
   </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in org_files_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:orgFiles:add']"
        >新增</el-button>
      </el-col>
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orgFilesList" @selection-change="handleSelectionChange">
      
      <el-table-column label="机构" align="center" prop="companyName" width="250" :show-overflow-tooltip="true"/>
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="org_files_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      
      <el-table-column label="模板名称" align="center" prop="templateName">
		  <template #default="scope">
		     <a v-bind:href="'https://' + scope.row.templateUrl" class="link-type">
		        <span>{{ scope.row.templateName }}</span>
		     </a>
		  </template>
		  </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum" />
	  <el-table-column label="默认份数" align="center" prop="defaultNum" />
	  <el-table-column label="需要上传文件" align="center" prop="isFile">
	    <template #default="scope">
	  		  <span v-if="scope.row.isFile==0">是</span>
	  		  <span v-if="scope.row.isFile==1">否</span>
	    </template>
	  </el-table-column>
      <el-table-column label="是否需要回传" align="center" prop="isBack">
        <template #default="scope">
		  <span v-if="scope.row.isBack==0">是</span>
		  <span v-if="scope.row.isBack==1">否</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要法人章" align="center" prop="needLegalSeal">
        <template #default="scope">
			<span v-if="scope.row.needLegalSeal==0">是</span>
			<span v-if="scope.row.needLegalSeal==1">否</span>       
        </template>
      </el-table-column>
      <el-table-column label="是否需要公章" align="center" prop="needCompanySeal">
        <template #default="scope">
			<span v-if="scope.row.needCompanySeal==0">是</span>
			<span v-if="scope.row.needCompanySeal==1">否</span> 
        </template>
      </el-table-column>
      <el-table-column label="省" align="center" prop="province" >
		  <template #default="scope">
		  	<span v-if="scope.row.province==null">通用</span>
		  	<span v-else>{{scope.row.province}}</span> 
		  </template>
	  </el-table-column>
      <el-table-column label="市" align="center" prop="city">
		  <template #default="scope">
		  	<span v-if="scope.row.city==null">通用</span>
		  	<span v-else>{{scope.row.city}}</span> 
		  </template>
	  </el-table-column>
     
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:orgFiles:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:orgFiles:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改材料包管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="orgFilesRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="机构" prop="tenantId">
         <el-select v-model="form.tenantId" style="width: 200px">
            <el-option
               v-for="item in orgOptions"
               :key="item.tenantId"
               :label="item.companyName"
               :value="item.tenantId"
            />
         </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in org_files_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板地址" prop="templateUrl">
          <file-upload v-model="form.templateUrl" :limit="1" :fileSize="10" :fileType="['doc','docx','xls', 'xlsx', 'pdf']"/>
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
		   <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
		<el-form-item label="默认份数" prop="defaultNum">
		   <el-input-number v-model="form.defaultNum" controls-position="right" :min="1" />
		</el-form-item>
		<el-form-item label="需要上传文件" prop="isFile">
		  <el-radio-group v-model="form.isFile">
		   <el-radio :label="0">是</el-radio>
		   <el-radio :label="1">否</el-radio>
		  </el-radio-group>
		</el-form-item>
        <el-form-item label="是否需要回传" prop="isBack">
          <el-radio-group v-model="form.isBack">
           <el-radio :label="0">是</el-radio>
           <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
       <el-form-item label="是否需要法人章" prop="needLegalSeal">
          <el-radio-group v-model="form.needLegalSeal">
         <el-radio :label="0">是</el-radio>
         <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否需要公章" prop="needCompanySeal">
          <el-radio-group v-model="form.needCompanySeal">
           <el-radio :label="0">是</el-radio>
           <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
      <el-form-item class="form-item" label="区域(不选为通用)" prop="citycode">
          <el-cascader v-model="form.citycode" :options="cityOpt" :props="CascaderProps"  @change="($event) => { cityChange($event, 1) }" clearable filterable/>
      </el-form-item>
        <el-form-item label="合同模板说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入合同模板说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrgFiles">
import { listOrgFiles, getOrgFiles, delOrgFiles, addOrgFiles, updateOrgFiles } from "@/api/system/orgFiles";
import { getOrgTenantList } from "@/api/system/tenant";
import { regionList } from "@/api/tool/region";
const orgRegionList = ref([]);
const orgOptions = ref([]);
const { proxy } = getCurrentInstance();
const {org_files_type } = proxy.useDict('sys_yes_no', 'org_files_type');

const orgFilesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
	// 省市区 参数
	CascaderProps: {
		    value: 'name',
		    label: 'name',
		    children: 'list',
		},
	form: {},
	cityOpt: [],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tenantId: null,
    type: null,
    templateUrl: null,
    templateName: null,
    orderNum: null,
    isBack: null,
    needLegalSeal: null,
    needCompanySeal: null,
    province: null,
    city: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules,cityOpt,CascaderProps } = toRefs(data);

/** 查询材料包管理列表 */
function getList() {
  loading.value = true;
  listOrgFiles(queryParams.value).then(response => {
    orgFilesList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}
function getOrgList() {
    getOrgTenantList().then(response => {
    orgOptions.value = response.data;
  });
}
// 所有省市区
function getRegionList() {
    regionList({ threeLevel: 2 }).then(response => {
        var data = response.data;
        cityOpt.value = data;
    });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orgFilesId: null,
    tenantId: null,
    type: null,
    templateUrl: null,
    templateName: null,
    orderNum: null,
	defaultNum: null,
	isFile: 1,
    isBack: 1,
    needLegalSeal: 1,
    needCompanySeal: 1,
    province: null,
    city: null,
    remark: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("orgFilesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}


function cityChange(e, type) {	
	form.value.province = form.value.citycode[0];
	form.value.city = form.value.citycode[1];
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orgFilesId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加材料包管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orgFilesId = row.orgFilesId || ids.value
  getOrgFiles(_orgFilesId).then(response => {
    form.value = response.data;
	form.value.citycode = [form.value.province, form.value.city];
    open.value = true;
    title.value = "修改材料包管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orgFilesRef"].validate(valid => {
    if (valid) {
		var formdata = JSON.parse(JSON.stringify(form.value));
		
		if(formdata.citycode==null)
		{
			formdata.province =null;
			formdata.city =null;
		}
      if (form.value.orgFilesId != null) {
        updateOrgFiles(formdata).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrgFiles(formdata).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orgFilesIds = row.orgFilesId || ids.value;
  proxy.$modal.confirm('是否确认删除材料包管理编号为"' + _orgFilesIds + '"的数据项？').then(function() {
    return delOrgFiles(_orgFilesIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/orgFiles/export', {
    ...queryParams.value
  }, `orgFiles_${new Date().getTime()}.xlsx`)
}

getList();
getOrgList();
getRegionList();
</script>
